<?php


namespace Api\Controller;


/** 
 * 搜索操作
 function:
 keywords  标题搜索
 tag       标签搜索
 * @author Legend. <xcx_legender@qq.com>
 * @param 
 * @return 
 */

class SearchController extends PublicController 
{


	/** 
	 * 标题搜索页面   
	 * @author Legend. <xcx_legender@qq.com>
	 * @param $title
	 * @return 
	 */
	
	public function keywords()
	{
		$title = I('title', '', 'htmlspecialchars');
		if (!$title)
		{
			$this->param_error();
		}
		event('Cache')->setSearchKey($title);
		$pager 	   = I('pager', C('HOME_VIDEO_PER',NULL, 5), 'intval');
		$timestamp    = I('timestamp',0, 'intval');
		$service = D('Video', 'Service');
		//start 数据调整title 更改为 full_title by susy.liang
		//$map['title'] = array('like', $title);
		$query[]['full_title'] = new \MongoRegex("/".$title."/i");
		$query[]['title'] = new \MongoRegex("/".$title."/i");
		$query[]['tags'] = $title;
		$map['$and'][]['$or'] = $query;
		//end
		$map['$and'][]['create_time'] = $timestamp ? array('$lt' => $timestamp) : array('$gt' => 0);
		$map['$and'][]['status'] = $service::STATUS_PASS;
		$where['_array'] = $map;
		$res   = $service->where($where)->order('create_time desc')->limit($pager)->field('id')->select();
		foreach ($res as $key => $value) {
			$ids[] = $value['id'];
		}
		$lists = $this->lists_format($ids);
		$lists = $lists ? $lists : array();
		$this->success('', $lists);
	}

	public function users()
	{
		$nickname = I('nickname', '', 'htmlspecialchars');
		if ($nickname === '')
		{
			$this->param_error();
		}
		event('Cache')->setSearchKey($nickname);
		$uid = 0;
		if (I('appToken')){
			$uid = $this->getUid(I('appToken'));
		}

		$map = [
			'status'   => 1,
			'nickname' => new \MongoRegex("/".$nickname."/i")
		];

		// $query['$and'][] = [
		// 	'status'   => 1,
		// 	'nickname' => new \MongoRegex("/".$nickname."/i")
		// ];

		// $map['_array'] = $query;

		$per 	= I('per', C('HOME_VIDEO_PER', null, 5), 'intval');
		$page 	= max(I('page',1, 'intval'), 1);
		$res = service('Users')->where($map)
				->limit($per)->page($page)
				->order('follows desc, uid asc')
				// ->field('uid')->select();
				// ->field('nickname, avatar, follows, uid as id, level, background, gender')
				->getField('id', true);
				// ->select();
		// echo service('Users')->_sql(); 
		$FollowEvent = event('Follow');
		// print_r($res);
		if ($res)
		{
			foreach ($res as $id) {
				if (!$id) continue;
				$user = event('User')->info($id);
				// uri_format($user['avatar']);
				// uri_format($user['background']);
				$user['follows'] or $user['follows'] = $FollowEvent->count_follows($user['id']);
				$uid && $user['is_follow'] = $FollowEvent->is_follow($uid, $user['id']) ? 1 : 0;
				$users[] = $user;
			}	
		}
		
		$this->list_success($users);
	}

	/** 
	 * 获取热门标签
	 * @author Legend. <xcx_legender@qq.com>
	 * @param 
	 * @return 
	 */
	
	public function tags()
	{
		$event = A('Api/Tag', 'Event');
		$data = $event->getHot();
		$this->success('', $data);
	}

	// 获取热门搜索关键词
	public function hot()
	{
		$per = I('per', 5, 'intval');
		$list = event('Cache')->getSearchKeyHot($per);
		$this->list_success($list);
	}

	/**
	 * 关键字搜索视频API
	 * 可选择排序方式，热门/最新
	 * @author   liubo <liubo@ilongyuan.com.cn>
	 */
	public function video()
	{
		$pager 	   = I('pager', C('HOME_VIDEO_PER',NULL, 5), 'intval');
		$page_no   = I('page_no', 1, 'intval');

		//$order可选参数为create_time, play
		$order     = I('order');
		!in_array($order, ['craete_time', 'play']) && $order = 'create_time';

		$service = D('Video', 'Service');
		//start 数据调整title 更改为 full_title by susy.liang
		//$map['title'] = array('like', $title);

		$title = I('title', '', 'htmlspecialchars');
		$uid = I('uid', 0, 'intval');
		if ($title) {
			event('Cache')->setSearchKey($title);
			$query[]['full_title'] = new \MongoRegex("/".$title."/i");
			$query[]['title'] = new \MongoRegex("/".$title."/i");
			$query[]['tags'] = $title;
			$map['$and'][]['$or'] = $query;
		}
		$map['$and'][]['status'] = $service::STATUS_PASS;
		$uid && $map['$and'][]['uid'] = $uid;
		$where['_array'] = $map;
		$res   = $service->where($where)->order("$order desc")->limit(($page_no-1)*$pager, $pager)->field('id')->select();
		foreach ($res as $key => $value) {
			$ids[] = $value['id'];
		}
		$lists = $this->lists_format($ids);
		$lists = $lists ? $lists : array();
		$this->success('', $lists);
	}


	/** 
	 * 标签视频列表页
	 * @author Legend. <xcx_legender@qq.com>
	 * @param 
	 * @return 
	 */
	
	public function tag()
	{
		$tag = I('tag');
		if (!$tag)
		{
			$this->param_error();
		}
		$service = D('Common/Video', 'Service');
		$pager 	   = I('pager', C('HOME_VIDEO_PER',NULL, 5), 'intval');
		$timestamp    = I('timestamp',0, 'intval');
		$map['tags']   =  $tag;
		$map['status'] = $service::STATUS_PASS;
		$map['create_time'] = $timestamp ? array('lt', $timestamp) : array('gt', 0);
		$res   = $service->where($map)->order('create_time desc')->limit($pager)->field('id')->select();
		foreach ($res as $key => $value) {
			$ids[] = $value['id'];
		}
		$lists = $this->lists_format($ids);
		$lists = $lists ? $lists : array();
		$this->success('', $lists);
	}
}